package com.longrise.android.upload_offline.catlineplay;

import android.content.Context;
import android.content.SharedPreferences;

import com.longrise.common.utils.AppUtil;


/**
 * Created by sujizhong on 16/4/11.
 */
public class BBRedis {

    private final static String SP_NAME = "BBT_CUTLINE";
    private static SharedPreferences mPreferences;

    private static final String DURATION = "DURATIDON";

    private static SharedPreferences getSp() {
        if (mPreferences == null) {
            mPreferences = AppUtil.getContext().getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
        }
        return mPreferences;
    }

    public static void addPosition(String key, int position, int duration) {
        SharedPreferences sp = getSp();
        SharedPreferences.Editor editor = sp.edit();
        editor.putInt(key, position);
        editor.putInt(key + DURATION, duration);
        editor.commit();
    }

    public static int getPosition(String key) {
        SharedPreferences sp = getSp();
        if (sp.contains(key)) {
            return sp.getInt(key, 0);
        }
        return 0;
    }

    public static int getDuration(String key) {
        SharedPreferences sp = getSp();
        return sp.getInt(key + DURATION, -1);
    }

    public static void removePosition(String key) {
        SharedPreferences sp = getSp();
        if (!sp.contains(key) && !sp.contains(key + DURATION)) {
            return;
        }
        SharedPreferences.Editor editor = sp.edit();
        editor.remove(key);
        editor.remove(key + DURATION);
        editor.commit();
    }

    public static boolean removeRecords(String key){
        SharedPreferences sp = getSp();
        SharedPreferences.Editor editor = sp.edit();
        editor.remove(key);
        return editor.commit();
    }

    public static void add(String key, int value) {
        SharedPreferences sp = getSp();
        SharedPreferences.Editor editor = sp.edit();
        editor.putInt(key, value);
        editor.commit();
    }

    public static int get(String key) {
        SharedPreferences sp = getSp();
        return sp.getInt(key, 0);
    }

    public static String getString(String key){
        SharedPreferences sp = getSp();
        return sp.getString(key, "");
    }

    public static void add(String key, String str) {
        SharedPreferences sp = getSp();
        SharedPreferences.Editor editor = sp.edit();
        editor.putString(key, str);
        editor.commit();
    }
}
